using Blog2025.Application.Common;
using Blog2025.Application.Dto;
using Blog2025.Domain.Entities.App;
using Blog2025.Domain.Filters;
using Blog2025.Domain.Repositories;

namespace Blog2025.Application.Services;

public class RoleAppServices : IRoleAppServices
{
    private readonly IRepository<AppRole> _RoleRep;
    private readonly IRepository<AppPermission> _PerRep;
    private readonly IFilter<AppRole> _filter;
    public RoleAppServices(IRepository<AppRole> RoleRep,IRepository<AppPermission> PerRep,IFilter<AppRole> filter)
    {
        _RoleRep=RoleRep;
        _PerRep=PerRep;
        _filter=filter;
    }
    public async Task CreateRoleAsync(CreateRole dto)
    {
        var role=new AppRole{RoleName=dto.RoleName,DataScope=dto.DataScope};
        await _RoleRep.CreateAsync(role);
    }

    public async Task DeleteRoleAsync(Guid id)
    {
        var obj=await _RoleRep.GetByIdAsync(id);
        await _RoleRep.DeleteAsync(obj);
    }

    public async Task<dynamic> GetAllRoleAsync(PageRequestDto pageRequestDto)
    {
        var obj = await _RoleRep.GetPagedAsync(pageRequestDto.PageIndex,pageRequestDto.PageSize);
        int totalCount=obj.Count();
        int totalPage=(int)Math.Ceiling((double)obj.Count() / pageRequestDto.PageSize);
        int pageIndex=pageRequestDto.PageIndex;
        int pageSize=pageRequestDto.PageSize;
        var items=obj.Select(r => new RoleDto { Id = r.Id, RoleName = r.RoleName, DataScope = r.DataScope, Code = r.Code});
        return PageResult.Success(totalCount,totalPage,pageIndex,pageSize,items);
    }
    // 过滤器
    public async Task<dynamic> GetFilterAsync(Dictionary<string ,object> filters)
    {
        var roles=await _RoleRep.GetAllAsync();
        return _filter.Filter(roles,filters);
    }

    public async Task<RoleDto> GetByIdRoleAsync(Guid id)
    {
        var obj=await _RoleRep.GetByIdAsync(id)?? throw new Exception("角色不存在");
        return new RoleDto{Id = obj.Id, RoleName = obj.RoleName, DataScope = obj.DataScope};
    }

    public async Task UpdateRoleAsync(Guid id ,UpdateRoleDto dto)
    {
        var obj=await _RoleRep.GetByIdAsync(id)?? throw new Exception("角色不存在");
        obj.RoleName=dto.RoleName;
        obj.DataScope=dto.DataScope;
        await _RoleRep.UpdateAsync(obj);
    }

    // 添加权限
    public async Task<dynamic> AssignPermission(Guid id,string perCode)
    {
        var obj=await _RoleRep.GetByIdAsync(id)?? throw new Exception("角色不存在"); 
        var per=(await _PerRep.GetAllAsync()).FirstOrDefault(p=>p.Code==perCode);
        // var per=_PerRep.Table.FirstOrDefault(p=>p.Code==perCode);
        if(per==null)
        {
            return ApiResult.Fail(404, "未找到对应的权限");
        }
        obj.AssignPermission(per);
        await _RoleRep.UpdateAsync(obj);
        return ApiResult.Success(obj,"添加权限成功");
    }
    // 移除权限
    public async Task<dynamic> RemovePermission(Guid id,string perCode)
    {
        var obj=await _RoleRep.GetByIdAsync(id)?? throw new Exception("角色不存在");
        var per=(await _PerRep.GetAllAsync()).FirstOrDefault(p=>p.Code==perCode);
        // var per=_PerRep.Table.FirstOrDefault(p=>p.Code==perCode);
        if(per==null)
        {
            return ApiResult.Fail(404, "未找到对应的权限");
        }
        obj.AssignPermission(per);
        await _RoleRep.UpdateAsync(obj);
        return ApiResult.Success(obj,"删除权限成功");
    }
}